#!/usr/bin/python2.7
# _*_ coding: utf-8 _*_

"""
@Author: MarkLiu
"""
import AdaboostNavieBayes as boostNaiveBayes
import random
import time
import numpy as np
# encoding=utf8
import sys
from config import *
reload(sys)
from multiprocessing import Process, Queue
sys.setdefaultencoding('utf8')
def trainingAdaboostGetDS(iterateNum=1):
    """
    测试分类的错误率
    :param iterateNum:
    :return:
    """
    smsWords = []
    f = open(sms_word_file)
    for line in f.readlines():
        linedatas = line.strip().split(SPLIT_TAB)
        smsWords.append(linedatas)
    classLables = np.loadtxt(sms_category_file,delimiter=SPLIT_SPECIAL)
    # 验证
    testWords = ["Free"]
    testWordsType = [0]
    testCount = 1
    '''
    for i in range(testCount):
        randomIndex = int(random.uniform(0, len(smsWords)))
        testWordsType.append(classLables[randomIndex])
        testWords.append(smsWords[randomIndex])
        #del (smsWords[randomIndex])
        #del (classLables[randomIndex])
    '''
    """
    训练阶段，可将选择的vocabularyList也放到整个循环中，以选出
    错误率最低的情况，获取最低错误率的vocabularyList
    """
    #vocabularyList = boostNaiveBayes.createVocabularyList(smsWords)
    vocabularyList = []
    f = open(sms_vocalist_file)
    for line in f.readlines():
        for word in line.strip().split(SPLIT_TAB):
            vocabularyList.append(word)
    print "读取语料库完成！"
    trainMarkedWords = []
    trainMarkedWords =boostNaiveBayes.setOfWordsListToVecTor(vocabularyList,smsWords)
    #f = open(sms_tmp_file)
    #for line in f.readlines():
    #    trainMarkedWords.append(line.split(SPLIT_TAB))
    # 转成array向量
    trainMarkedWords = np.array(trainMarkedWords)
    print "数据转成矩阵！"
    pWordsSpamicity, pWordsHealthy, pSpam = \
        boostNaiveBayes.trainingNaiveBayes(trainMarkedWords, classLables)
    DS = np.ones(len(vocabularyList))
    ds_errorRate = {}
    minErrorRate = np.inf
    '''
    for i in range(iterateNum):
        errorCount = 0.0
        for j in range(testCount):
            testWordsCount = boostNaiveBayes.setOfWordsToVecTor(vocabularyList, testWords[j])
            ps, ph, smsType = boostNaiveBayes.classify(pWordsSpamicity, pWordsHealthy,
                                                       DS, pSpam, testWordsCount)
            print testWords[j]
            print smsType
            if smsType != testWordsType[j]:
                errorCount += 1
                # alpha = (ph - ps) / ps
                alpha = ps - ph
                # if alpha < 0:  # 原先为spam，预测成ham， ERROR!
                if alpha > 0: # 原先为ham，预测成spam
                    DS[testWordsCount != 0] = np.abs(
                            (DS[testWordsCount != 0] - np.exp(alpha)) / DS[testWordsCount != 0])
                # else:  # 原先为ham，预测成spam，ERROR
                else:  # 原先为spam，预测成ham
                    DS[testWordsCount != 0] = (DS[testWordsCount != 0] + np.exp(alpha)) / DS[testWordsCount != 0]
        #print 'DS:', DS
        errorRate = errorCount / testCount
        if errorRate < minErrorRate:
            minErrorRate = errorRate
            ds_errorRate['minErrorRate'] = minErrorRate
            ds_errorRate['DS'] = DS
        #print '第 %d 轮迭代，错误个数 %d ，错误率 %f' % (i+1, errorCount, errorRate)
        if errorRate == 0.0:
            break
    '''
    ds_errorRate['minErrorRate'] = minErrorRate
    ds_errorRate['DS'] = DS
    ds_errorRate['vocabularyList'] = vocabularyList
    ds_errorRate['pWordsSpamicity'] = pWordsSpamicity
    ds_errorRate['pWordsHealthy'] = pWordsHealthy
    ds_errorRate['pSpam'] = pSpam
    return ds_errorRate


if __name__ == '__main__':
    start = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    dsErrorRate = trainingAdaboostGetDS()
    # 保存模型训练的信息
    np.savetxt(sms_spam_file, dsErrorRate['pWordsSpamicity'], delimiter=SPLIT_TAB)
    np.savetxt(sms_ham_file, dsErrorRate['pWordsHealthy'], delimiter=SPLIT_TAB)
    np.savetxt(spam_file, np.array([dsErrorRate['pSpam']]), delimiter=SPLIT_TAB)
    np.savetxt(sms_ds_file, dsErrorRate['DS'], delimiter=SPLIT_TAB)
    #np.savetxt('trainMinErrorRate.txt', np.array([dsErrorRate['minErrorRate']]), delimiter='\t')
    #vocabulary = dsErrorRate['vocabularyList']
    #fw = open('vocabularyList.txt', 'w')
    #for i in range(len(vocabulary)):
    #    fw.write(vocabulary[i] + '\t')
    #fw.flush()
    #fw.close()
    end = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    print "开始时间:%s" % start
    print "结束时间:%s" % end
    print "学习完成"
